#include "drive_led.h"

extern TIM_HandleTypeDef htim3;

LampLevel_t LED_level;

/**
  * @brief  USB LED模块初始化
  * @note   启动PWM输出
  */
void LED_Init(void)
{
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 0); // 默认关闭
}

/**
  * @brief  设置USB LED占空比
  * @param  duty: PWM占空比(0~999)
  */
void LED_SetDuty(uint16_t duty)
{
    if (duty > 999) duty = 998;
    __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, duty);
}

/**
  * @brief  设置USB LED亮度等级
  * @param  level: LED亮度枚举值
  */
void LED_SetLevel(LampLevel_t level)
{
    uint16_t duty = 0;

    switch (level)
    {
        case LIGHT_OFF:    duty = 0;   break;
        case LIGHT_LOW:    duty = 250; break;
        case LIGHT_MEDIUM: duty = 500; break;
        case LIGHT_HIGH:   duty = 750; break;
        case LIGHT_MAX:    duty = 990; break;
        default:         duty = 0;   break;
    }

    LED_SetDuty(duty);
}
